package com.app.Activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import app.com.ModuleRole.R;

import com.app.Adapter.RuleAdapter;
import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;
import com.app.DTO.RoleDTO;
import com.app.PhanQuyen.Quyen;
import com.app.RowDTO.RuleRowDTO;

public class frmQuanLyRoleActivity extends Activity {
	final int MY_REQUEST_TAO_ROLE = 100;
	final int MY_REQUEST_SAVE_FILE=101;
	ListView lvDanhSachVaiTro;
	ArrayList<RoleDTO> listRole = null;
	String path=ReadWriteFileController.DIRECTORY + "myRole.xml";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("phu","onCreate");
		setContentView(R.layout.layout_quan_ly_role);
		lvDanhSachVaiTro = (ListView) findViewById(R.id.lvDanhSachVaiTro);
		fillListView();		
		 registerForContextMenu(lvDanhSachVaiTro);
		 
		 //this.setTitle(NetworkController._peer.getAddressPeer());
	}

	void fillListView() {

		
		
			listRole = ReadWriteFileController.readRole(path);
		
		ArrayAdapter<RoleDTO> adapter = new ArrayAdapter<RoleDTO>(this,
				android.R.layout.simple_list_item_1, listRole);
		lvDanhSachVaiTro.setAdapter(adapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(getApplication()).inflate(R.menu.omnu_quan_ly_vai_tro,
				menu);
		return (super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case R.id.mnuThemMoi:
			Intent intent = new Intent(getBaseContext(),
					frmTaoRoleActivity.class);			
			startActivityForResult(intent, MY_REQUEST_TAO_ROLE);
			break;
		case R.id.mnuLuaChon:
			break;
		case R.id.mnuSave:
			Intent fileDialog=new Intent(getBaseContext(),FrmSelectFile.class);
			startActivityForResult(fileDialog,MY_REQUEST_SAVE_FILE);
			break;
		default:
			break;
		}
		return true;
	}
	public void showToast(String message) {
		Toast t = Toast.makeText(this, message, Toast.LENGTH_LONG);
		t.show();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == MY_REQUEST_TAO_ROLE) {
			if (resultCode == RESULT_OK) {
				Bundle b = data.getExtras();
				RoleDTO role = (RoleDTO) b.getSerializable("Role");
				listRole.add(role);
			}
		}
		else if(requestCode==MY_REQUEST_SAVE_FILE){
			Bundle b = data.getExtras();
			String path=b.getString(FrmSelectFile.RESULT_PATH);
			ReadWriteFileController.writeRole(path, listRole);
			showToast("Đã lưu xuống tập tinh: "+path);
		}
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
	    ContextMenuInfo menuInfo) {
	  if (v.getId()==R.id.lvDanhSachVaiTro) {
	    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
	    menu.setHeaderTitle("thao tác");
	    menu.addSubMenu(Menu.NONE, 1, 1, "Xóa");
	    menu.addSubMenu(Menu.NONE,2,2,"Cập nhật");
	  }
	}
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
	  int menuItemIndex = item.getItemId();	  
	  String text=listRole.get(info.position).getName();
	  if(menuItemIndex==1){
		  ReadWriteFileController.deleteRole(path, listRole.get(info.position));
		  fillListView();
	  }else if(menuItemIndex==2){
		  Intent intent=new Intent(this,frmTaoRoleActivity.class);
		  Bundle bundle=new Bundle();
		  bundle.putSerializable("Update", listRole.get(info.position));
		  intent.putExtras(bundle);
		  startActivity(intent);
	  }
	  return true;
	}
	@Override
	protected void onResume(){
		super.onResume();
		Log.d("phu", "onResume");
	}
	@Override
	protected void onPause(){
		super.onPause();
		Log.d("phu", "onPause");
	}
	@Override
	protected void onStop(){
		super.onStop();
		Log.d("phu", "onStop");
	}
	@Override
	protected void onRestart(){
		super.onRestart();
		Log.d("phu", "onRestart");
	}
	

}
